package cn.lunkr.sdkdemo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import cn.lunkr.sdkdemo.databinding.FragmentFirstBinding;
import cn.lunkr.sdkdemo.util.CoreMailHelper;
import cn.lunkr.sdkdemo.util.SPUtil;

public class FirstFragment extends Fragment {

    private static final String TAG = "FirstFragment";

    private FragmentFirstBinding binding;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentFirstBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
            }
        });

        binding.emailLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtil spUtil = SPUtil.getInstance();
                String token = spUtil.getString(SPUtil.TOKEN, null);
                String url = spUtil.getString(SPUtil.URL, null);
                String loginType = spUtil.getString(SPUtil.LOGIN_TYPE, "");

                if ("sso".equals(loginType) ? (TextUtils.isEmpty(token) || TextUtils.isEmpty(url)) : TextUtils.isEmpty(url)) {
                    setting();
                    return;
                }
                CoreMailHelper.getInstance().start(DemoApp.get());
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    void setting() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("请配置参数");
//         builder.setTitle("提示");
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(getActivity(), SettingsActivity.class));
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}